﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.Manage
{
    public partial class ExamItemsGroup : System.Web.UI.Page
    {
        static int schoolid = Config.CurrentSchoolID;
        static int examid = 0;
        static int examitemgroupid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var myEIG = from c in db.ExamItemsGroup
                                select new { SchoolID = c.SchoolID, ExamID = c.ExamID, GroupName = c.GroupName, ExamItemGroupID = c.ExamItemGroupID, ExamName = c.ExamInfo.ExamName }; ;
                    this.Store2.DataSource = myEIG;
                    this.Store2.DataBind();
                }
            }
        }

       

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            GroupName.Text = "";
            DropDownField3.Text = "请选择";
            //schoolid = 0;
            examid = 0;
            this.FormWindow.Visible = true;
            this.FormWindow.Show();
            Button6.Show();
            Button7.Hide();
        }

        protected void Button3_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    examitemgroupid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var neweig = from c in db.ExamItemsGroup
                                 where c.ExamItemGroupID == examitemgroupid
                                 select c;
                    ExamAnalysis.Model.ExamItemsGroup newe = neweig.First();
                    //var school = from s in db.SchoolInfo
                    //             where s.SchoolID == newe.SchoolID
                    //             select s.FullName;
                    //schoolid = Convert.ToInt32(newe.SchoolID);
                    //examid = newe.ExamID;
                    GroupName.Text = newe.GroupName;
                    //DropDownField2.Text = school.First().ToString();
                    DropDownField3.Text = newe.ExamInfo.ExamName;
                }
                this.FormWindow.Visible = true;
                this.FormWindow.Show();
                Button6.Hide();
                Button7.Show();
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个班级").Show();
            }
        }

        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    examitemgroupid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var neweig = from c in db.ExamItemsGroup
                                 where c.ExamItemGroupID == examitemgroupid
                                 select c;
                    ExamAnalysis.Model.ExamItemsGroup newe = neweig.First();
                    db.ExamItemsGroup.DeleteOnSubmit(newe);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    Response.Redirect("ExamItemsGroup.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个分组").Show();
            }
        }

        //protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
        //{
        //    RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
        //    foreach (SelectedRow row in sm.SelectedRows)
        //    {
        //        schoolid = Convert.ToInt32(row.RecordID);
        //    }
        //    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
        //    {
        //        var myShool = from c in db.SchoolInfo
        //                      where c.SchoolID == schoolid
        //                      select c.FullName;
        //        foreach (var school in myShool)
        //        {
        //            DropDownField2.Text = school.ToString();
        //        }
        //    }
        //}

        protected void Button_5_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                examid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myExam = from c in db.ExamInfo
                              where c.ExamID == examid
                              select c.ExamName;
                foreach (var exam in myExam)
                {
                    DropDownField3.Text = exam.ToString();
                }
            }
        }

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            //if (DropDownField2.Text.ToString().Equals("请选择")||DropDownField3.Text.ToString().Equals("请选择"))
            //{
            //    X.Msg.Alert("提示信息", String.Format("请保持信息完整")).Show();
            //}
            //else
            //{
                ExamAnalysis.Model.ExamItemsGroup neweig = new ExamAnalysis.Model.ExamItemsGroup();
                neweig.GroupName = GroupName.Text;
                neweig.ExamID = examid;
                neweig.SchoolID = schoolid;
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    db.ExamItemsGroup.InsertOnSubmit(neweig);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", String.Format("成功添加分组")).Show();
                   // schoolid = 0;
                    examid = 0;
                }
                Response.Redirect("ExamItemsGroup.aspx");
           // }
        }

        protected void Change_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var neweig = from c in db.ExamItemsGroup
                             where c.SchoolID == schoolid && c.ExamID == examid
                             select c;
                ExamAnalysis.Model.ExamItemsGroup newe = neweig.First();
                newe.GroupName = GroupName.Text;
                newe.ExamID = examid;
                newe.SchoolID = schoolid;
                db.SubmitChanges();
                X.Msg.Alert("提示信息", String.Format("成功修改分组信息")).Show();
                //schoolid = 0;
                examid = 0;
                examitemgroupid = 0;
            }
            Response.Redirect("ExamItemsGroup.aspx");
        }
    }
}